package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author Yzc
 */
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;
	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	public void addUserAccount(Long userId) {
		// user_account
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccountMapper.insert(userAccount);
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		//	根据用户Id 获取到用户余额对象
		UserAccount userAccount = this.getUserAccountByUserId(userId);
		return userAccount.getAvailableAmount();
	}

	@Override
	public UserAccount getUserAccountByUserId(Long userId) {
		return this.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int checkAndDeduct(AccountDeductVo accountDeductVo) {
		//  调用mapper 层方法.
		int count = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(),accountDeductVo.getAmount());
		//  判断
		if (count == 0 ){
			throw new GuiguException(400, "账户余额不足！");
		}
		//  记录明细
		this.addUserAccountDetail(
				accountDeductVo.getUserId(),
				accountDeductVo.getContent(),
				SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,
				accountDeductVo.getAmount(),
				accountDeductVo.getOrderNo()
		);
		//  返回受影响的行数
		return count;
	}

	//记录用户账户信息
	private void addUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		userAccountDetailMapper.insert(userAccountDetail);
	}

	@Override
	public void add(Long userId, BigDecimal amount, String orderNo, String tradeType, String title) {
		// 查看是否有当前对象
		long count = userAccountDetailMapper.selectCount(new LambdaQueryWrapper<UserAccountDetail>()
				.eq(UserAccountDetail::getOrderNo, orderNo));
		if(count > 0) return;
		//添加账号金额
		userAccountMapper.add(userId, amount);
		//添加账户明细
		this.log(userId, title, tradeType, amount, orderNo);
	}

	private void log(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {

	}

	@Override
	public IPage<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> pageParam, Long userId) {
		// 调用mapper 方法
		return userAccountDetailMapper.selectUserRechargePage(pageParam,userId);
	}

	@Override
	public IPage<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> pageParam, Long userId) {
		// 调用mapper 层方法
		return userAccountDetailMapper.selectUserConsumePage(pageParam, userId);
	}
}